<template>
<div>
  <h1>登录页面</h1>

  <van-tabs v-model="active">
    <van-tab title="用户名登录">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit" @click='login'>提交</van-button>
      </div>

    </van-tab>
    <van-tab title="手机号登录">

      <van-form @submit="onSubmit">
        <van-field
          v-model="mobile"
          name="mobile"
          label="手机号"
          placeholder="手机号"
          :rules="[{ required: true, message: '请填写手机号' }]"
        />
        <van-button type="primary" @click='sendSms'>发送验证码</van-button>
        <van-field
          v-model="code"
          name="code"
          label="验证码"
          placeholder="验证码"
          :rules="[{ required: true, message: '请填写验证码' }]"
        />
        <div style="margin: 16px;">
          <van-button round block type="info" native-type="submit" @click='mlogin'>提交</van-button>
        </div>
      </van-form>
    </van-tab>

  </van-tabs>

</div>
</template>

<script>
export default {
  name: "login",
  data() {
    return {
      active: 1,
      username:'',
      password:'',
      mobile:'',
      code: '',
      onSubmit:"",
    };
  },
  methods: {
    login() {
      this.$axios.post('users/login',{'username':this.username,'password':this.password})
      .then(i=>{
        if(i.data.code ==200){
          localStorage.setItem('token',i.data.token)
          localStorage.setItem('username',i.data.username)
          localStorage.setItem("userid",i.data.userid)
          alert("登录成功")
          this.$router.push('user_info/')
        }
        else {alert(i.data.mes)}

      })
    },
    mlogin() {
      this.$axios.post('users/mlogin',{'mobile':this.mobile,'code':this.code})
      .then(i=>{
        if (i.data.code==200){
          alert('登录成功')

        }
        else {
          alert(i.data.mes)
        }
      })
    },
    //发送验证码
    sendSms() {
      this.$axios.post('users/sendsms', {'mobile': this.mobile}).then(res => {
        if (res.data.code == 200) {
          alert(res.data.smscode)
        } else {
          alert(res.data.mes)
        }
      })
    }
  },

}
</script>

<style scoped>

</style>
<template>
  <div>
    <div>
      <myheader/>
      <van-form @submit="onSubmit">
        <van-field
          v-model="form.username"
          name="username"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="form.password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <van-field
          v-model="form.mobile"
          name="mobile"
          label="手机号"
          placeholder="手机号"
          :rules="[{ required: true, message: '请填写手机号' }]"
        />
        <drag-verify
          :width="320"
          :height="42"
          text="请将滑块拖动到右侧"
          successText="验证成功"
          ref="Verify"
        >
        </drag-verify>

        <div style="margin: 16px;width:150px;">
          <van-button round block type="info" native-type="submit">提交</van-button>
        </div>
      </van-form>
    </div>

  </div>
</template>

<script>
import dragVerify from 'vue-drag-verify'
import myheader from "../myheader";


export default {
  name: "user",
  data() {
    return {
      form:{},
      width: 320,
      height: 42,

    };
  },
  methods: {
    onSubmit() {
      if(this.$refs.Verify.isPassing){
        this.$axios.post('users/reg',this.form).then(res=>{
          console.log(res)
          this.form = {}
          alert("添加成功")
        })
      }
      else {
        alert('请验证后提交')
      }
    },
  },
  components:{
    'myheader':myheader,
    dragVerify,
  }
}
</script>

<style scoped>

</style>
 
<template>
  <div>
    <myheader :img_src='img '/>
    用户中心<br />
    用户名:{{ user.username }}<br />
    手机号:{{ user.mobile | mobilefilter }}<br />
    头像<br /><van-image
    round
  width="20rem"
  height="20rem"
  :src="img"
/>
    上传头像：<van-uploader :after-read="afterRead" />
  </div>
</template>

<script>
import myheader from "../myheader";
export default {
  data() {
    return {
      user: {},
      userid: localStorage.getItem("userid"),
      img:''
    };
  },
  filters: {
    mobilefilter(value) {
        //把手机号给隐藏
        if(value){
          return value.slice(0, 3) + "******" + value.slice(-3, -1);
        }

    },
  },
  methods: {
    get_info() {
        //根据用户id查用户信息
      this.$axios.get("/users/get_info?userid=" + this.userid).then((res) => {
        this.user = res.data.user
        this.img ='http://localhost:5000/static/upload/'+this.userid+'/'+ res.data.user.avater
        ;
      });
    },
    afterRead(file) {
      // 此时可以自行将文件上传至服务器
      //传文件 重新构造一个data
      let data = new FormData()
      data.append('file',file.file)
      data.append('userid',this.userid)
      this.$axios.post('/users/upload',data)
      .then(res=>{
          if(res.data.code==200){
              alert('添加成功')
              // this.img = 'http://localhost:5000/static/upload/'+ res.data.filename

          }

      })
      console.log(file);
    },
  },
  components: {
    myheader: myheader,
  },

  mounted() {
    this.get_info();
  },
};
</script>

<style scoped>
</style>